package com.baselibrary.util.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * Created by jye on 2017/4/26.
 */
public class GlideUtils {
    /**
     * setRoundImageView
     */
    public static void setRoundImageView(final Context context, String imgUrl, int defaultResID, final ImageView imageView) {
        try {

            GlideApp.with(context).asBitmap().load(imgUrl).placeholder(defaultResID).centerCrop().into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });

        } catch (Exception e) {

            GlideApp.with(context).asBitmap().load(defaultResID).placeholder(defaultResID).centerCrop().into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
        }
    }

    /**
     * setRoundImageView
     */
    public static void setRoundImageViewWithBorder(final Context context, String imgUrl, int defaultResID, final ImageView imageView, int borderColor) {
        try {

            GlideApp.with(context).load(imgUrl).centerCrop().placeholder(defaultResID)
                    .transform(new com.quanshu.exchange.support.utils.glide.GlideCircleTransform(context, 4, context.getResources().getColor(borderColor)))
                    .into(imageView);

        } catch (Exception e) {

            GlideApp.with(context).load(defaultResID).centerCrop().placeholder(defaultResID)
                    .transform(new com.quanshu.exchange.support.utils.glide.GlideCircleTransform(context, 4, context.getResources().getColor(borderColor)))
                    .into(imageView);
        }
    }

    public static void setImageViewByThumb(final Context context, String imgUrl, int defaultResID, final ImageView imageView) {
        try {

            GlideApp.with(context).load(imgUrl).thumbnail(0.3f).placeholder(defaultResID).into(imageView);

        } catch (Exception e) {

            GlideApp.with(context).load(defaultResID).thumbnail(0.3f).placeholder(defaultResID).into(imageView);
        }
    }

    public static void setImageViewNoThumb(final Context context, String imgUrl, int defaultResID, final ImageView imageView) {
        try {

            GlideApp.with(context).load(imgUrl).placeholder(defaultResID).centerCrop().into(imageView);

        } catch (Exception e) {

            GlideApp.with(context).load(defaultResID).placeholder(defaultResID).centerCrop().into(imageView);
        }
    }



}
